﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WiiMote
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        string[] buttons = {
                               "A", 
                               "B",
                               "Minus",
                               "Plus",
                               "Home",
                               "One",
                               "Two",
                               "Up",
                               "Down",
                               "Left",
                               "Right",
                               "Shake Up/Down",
                               "Shake Left/Right",
                               "Tilt Left",
                               "Tilt Right"
                           };
        string[] values = {
                                "Left Click",
                                "Right Click",
                                "Task Manager",
                                "On-Screen Keyboard",
                                "Internet Browser",
                                "My Documents",
                                "Enter",
                                "Up",
                                "Down",
                                "Left",
                                "Right",
                                "Scroll Up",
                                "Scroll Down",
                                "Volume Up",
                                "Volume Down"
                            };
        string[] valueMap = {
                                "0x0002~0x0004",
                                "0x0008~0x0010",
                                "taskmgr",
                                "osk",
                                "http://www.google.com",
                                "explorer",
                                "{ENTER}",
                                "{UP}",
                                "{DOWN}",
                                "{LEFT}",
                                "{RIGHT}",
                                "{PGUP}",
                                "{PGDN}",
                                "idk",
                                "idk"
                            };

        string[] settings = File.ReadAllLines("settings.config");


        private void Form2_Load(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange(values);

            for (int i = 0; i < buttons.Length; i++)
            {
                dataGridView1.Rows.Add(buttons[i], settings[i]);
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count != 0)
            {
                comboBox1.Text = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[1].Value.ToString();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[1].Value = comboBox1.Text;
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            string newSettings = "";
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int f = 0; f < values.Length; f++)
                {
                    if (dataGridView1.Rows[i].Cells[1].Value.ToString() == values[f])
                    {
                        newSettings = newSettings + valueMap[f] + Environment.NewLine;
                        //newSettings = newSettings + dataGridView1.Rows[i].Cells[1].Value.ToString() + Environment.NewLine;
                        f = values.Length;
                    }
                }
                
            }
            File.WriteAllText("settings.config", newSettings);
        }
    }
}
